import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;

public class GUIv1 extends JApplet implements ActionListener
{
	//---------------------------------------------
	// Needed to please eclipse for some reason
	//
	private static final long serialVersionUID = 1L;
	
	//--------------------------------------------
    // combined to create the output window
    //
	JTextPane output;
	JScrollPane scrollPane;
	
	//--------------------------------------------
    // controls declarations
    //
	private JPanel controls;
	private JTextField studentFirstName;
	private JTextField studentLastName;
	private JTextField schoolName;
	private JTextField major;
	private JTextField gpaRange;
	private JTextField gpaRange2;
	private JTextField semesterRange;
	private JTextField semesterRange2;
	private JComboBox degreeLevelBox;
	private JComboBox collegeChoiceBox;
	private JButton submit;
	
	private JPanel optionPanel;
	private JCheckBox studentNameBox;
	private JCheckBox schoolNameBox;
	private JCheckBox majorBox;
	private JCheckBox gpaBox;
	private JCheckBox semesterBox;
	private JCheckBox degreeBox;
	private JCheckBox ssnBox;
	private JCheckBox addressBox;
	
	public void init()
	{		
		this.setSize(new Dimension(720, 780));
	}
	
	public GUIv1()
	{
		//-------------------------------------------
        //Create a the screen
		//
        JPanel screen = new JPanel(new BorderLayout());
        scrollPane = new JScrollPane();
        output = new JTextPane();
        scrollPane.getViewport().add(output);
        
        screen.setBackground(new Color(255, 255, 255));
        screen.setSize(new Dimension(540, 360));
        
        screen.add(scrollPane, BorderLayout.CENTER);
        
        setupReturn();
        setupControls();
        
        //-------------------------------------------
        // Set the menu bar and add the label to the content pane.
        //        
        getContentPane().add(screen);
        getContentPane().add(controls, BorderLayout.SOUTH);
        
        //-------------------------------------------
        // Amusing introductory message goes here
        //
        printToScreen("v1.3 \nGentlemen.\n\n");
	}
	
	public void printToScreen(String text)
	{
		try
		{
			output.getDocument().insertString(output.getDocument().getLength(), text, null);
			output.setCaretPosition(output.getDocument().getLength());
		}
		catch (BadLocationException e)
		{
			e.printStackTrace();
		}
	}
	
	//-------------------------------------------
    // Creates all the buttons
    //
	private void setupControls()
	{
		controls = new JPanel();
		
		controls.setLayout(new BoxLayout(controls, BoxLayout.Y_AXIS));
		
		controls.add(optionPanel);
		
		JPanel studentNamePanel = new JPanel();
		studentNamePanel.setBorder(BorderFactory.createTitledBorder("Student Name"));
		
		JLabel nameLabelFirst = new JLabel("First");
		JLabel nameLabelLast = new JLabel("Last");
		studentFirstName = new JTextField(15);
		studentLastName = new JTextField(15);
		studentNamePanel.add(nameLabelFirst);
		studentNamePanel.add(studentFirstName);
		studentNamePanel.add(nameLabelLast);
		studentNamePanel.add(studentLastName);
		
		controls.add(studentNamePanel);
		
		JPanel schoolNamePanel = new JPanel();
		schoolNamePanel.setBorder(BorderFactory.createTitledBorder("School Name"));
		
		schoolName = new JTextField(20);
		schoolNamePanel.add(schoolName);
		controls.add(schoolNamePanel);
		
		JPanel majorPanel = new JPanel();
		majorPanel.setBorder(BorderFactory.createTitledBorder("Major"));
		
		major = new JTextField(20);
		majorPanel.add(major);
		controls.add(majorPanel);
		
		JPanel degreePanel = new JPanel();
		degreePanel.setBorder(BorderFactory.createTitledBorder("Degree Level"));
		
		String[] degreeStrings = { "Degree N/A", "BS", "MS", "PHD" };
		degreeLevelBox = new JComboBox(degreeStrings);
		degreeLevelBox.setSelectedIndex(0);
		degreeLevelBox.addActionListener(this);
		
		degreePanel.add(degreeLevelBox);
		controls.add(degreePanel);
		
		JPanel gpaPanel = new JPanel();
		gpaPanel.setBorder(BorderFactory.createTitledBorder("GPA range"));
		
		gpaRange = new JTextField(5);
		gpaRange2 = new JTextField(5);
		JLabel gpaLabel = new JLabel(" to ");
		gpaPanel.add(gpaRange);
		gpaPanel.add(gpaLabel);
		gpaPanel.add(gpaRange2);
		controls.add(gpaPanel);
		
		JPanel semesterPanel = new JPanel();
		semesterPanel.setBorder(BorderFactory.createTitledBorder("Semester range"));
		
		semesterRange = new JTextField(8);
		semesterRange2 = new JTextField(8);
		JLabel semesterLabel = new JLabel(" to ");
		semesterPanel.add(semesterRange);
		semesterPanel.add(semesterLabel);
		semesterPanel.add(semesterRange2);
		controls.add(semesterPanel);
		
		JPanel collegePanel = new JPanel();
		collegePanel.setBorder(BorderFactory.createTitledBorder("College Choice"));
		
		String[] collegeStrings = { "College N/A", "Engineering", "Liberal Arts", "Science", "Business", "Agriculture" };
		collegeChoiceBox = new JComboBox(collegeStrings);
		collegeChoiceBox.setSelectedIndex(0);
		collegeChoiceBox.addActionListener(this);
		
		collegePanel.add(collegeChoiceBox);
		controls.add(collegePanel);
		
		submit = new JButton("Search");
		controls.add(submit, BorderLayout.PAGE_END);
		submit.addActionListener(this);
	}
	
	//-------------------------------------------
    // Creates the return type selection menu
    //
	private void setupReturn()
	{
		
		optionPanel = new JPanel();
		optionPanel.setBorder(BorderFactory.createTitledBorder("Select return parameters"));
		
		studentNameBox = new JCheckBox("Student Name");
		optionPanel.add(studentNameBox);
		
		schoolNameBox = new JCheckBox("School");
		optionPanel.add(schoolNameBox);
		
		majorBox = new JCheckBox("Major");
		optionPanel.add(majorBox);
		
		gpaBox = new JCheckBox("GPA");
		optionPanel.add(gpaBox);
		
		semesterBox = new JCheckBox("Semesters");
		optionPanel.add(semesterBox);
		
		degreeBox = new JCheckBox("Degrees");
		optionPanel.add(degreeBox);
		
		ssnBox = new JCheckBox("SSN");
		optionPanel.add(ssnBox);
		
		addressBox = new JCheckBox("Address");
		optionPanel.add(addressBox);
	}
	public void actionPerformed(ActionEvent action) 
	{
		//-----------------------------------------
		// student first and last names are now selected with studentFirstName and studentLastName
		// additions are collegeChoiceBox, ssnBox, and addressBox
		//
		
		RunDB runDB = new RunDB();
		String text="Select ";
		if (studentNameBox.isSelected())
		{
			text += " students.Lastname, students.FirstName,";
		}
		if (schoolNameBox.isSelected())
		{
			text += " universities.school_name,";
		}
		if (majorBox.isSelected())
		{
			text += " degrees.degree_abv,"; 
		}
		if (gpaBox.isSelected())
		{
			text += " history.GPA,";
		}
		if (semesterBox.isSelected())
		{
			text += " semesters.semester,";
		}
		if (degreeBox.isSelected())
		{
			text += " history.degree_level,";
		}
		if(ssnBox.isSelected())
			text += " students.SSN,";
		
		if(addressBox.isSelected())
			text += " students.address,";

		text = text.substring(0, text.length()-1);
//		text += " 1 "; 
		//System.out.println(text.length());
		text += " from Colleges,Degrees,History, Semesters,students, Universities where " +
				" students.student_id = history.student_id and " +
				" students.student_id = semesters.student_id and " +
				" universities.university_id = semesters.university_id and " +
				" universities.university_id = history.university_id and " +
				" history.history_id = semesters.history_id and " +
				" history.student_id = semesters.student_id and " +
				" history.university_id = semesters.university_id and " +
				" history.college_id = colleges.college_id and " +
				" history.degree_id = degrees.degree_id and " +
				" colleges.college_id = degrees.college_id ";
		try
		{
			int u=0;
			if (action.getSource() == submit)
			{
				if(studentFirstName.getText().length()>0){
					text+=" and students.FirstName= \'"+studentFirstName.getText()+"\' ";
				}
				if(studentLastName.getText().length()>0){
					text+=" and students.LastName= \'"+studentLastName.getText()+"\' ";
				}
				if(schoolName.getText().length()>0)
				{
					text+=" and universities.school_name =  \'"+schoolName.getText()+"\' ";
				}
				if(major.getText().length()>0)
				{
					text+=" and degrees.degree_abv = \'"+major.getText()+"\' ";
				}	
				if(gpaRange.getText().length()>0)
				{
					
					text+=" and history.gpa >=  "+gpaRange.getText()+" ";
					
				}
				if(gpaRange2.getText().length()>0)
				{
					
					text+=" and history.gpa <=  "+gpaRange2.getText()+" ";
					
				}
				if(semesterRange.getText().length()>0)
				{
					
					text+=" and semesters.semester >=  \'"+semesterRange.getText()+"\' ";
					
				}
				if(semesterRange2.getText().length()>0)
				{
					
					text+=" and semesters.semester <=  \'"+semesterRange2.getText()+"\' ";
					
				}
				
				if(degreeLevelBox.getSelectedIndex()>0) {
					text += " and History.Degree_Level = \'"+(String) degreeLevelBox.getSelectedItem()+"\' ";
				}
				if(collegeChoiceBox.getSelectedIndex()>0) {
					text += " and Colleges.College_Name = \'"+(String) collegeChoiceBox.getSelectedItem()+"\' ";
				}

				text += "\n\n";
				printToScreen(text);
				//printToScreen("Here\n");
		    	runDB.loadDriver();
		        runDB.makeConnection();
		        runDB.buildStatement();
		        runDB.executeQuery(this,text);

			}

		}
		catch (Exception e)
		{
			e.printStackTrace();
			//return;
		}
		
	}

	private String String(Object selectedItem) {
		// TODO Auto-generated method stub
		return null;
	}
}